//
//  XHVisitorView.swift
//  swift新浪微博
//
//  Created by 魏兴浩 on 15/11/25.
//  Copyright © 2015年 wxh. All rights reserved.
//

import UIKit
//访客视图
class XHVisitorView: UIView {
    
    override init(frame:CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    //swift默认所有的view都能通过xib、syoryboard 加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //准备UI
    private func prepareUI() {
        //添加子控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //添加约束
        //1,关闭Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        //创建约束对象
        /// iconView CenterX与父控件的CenterX重合
        // item: 要添加约束的view
        // attribute: 设置的约束
        // relatedBy: Equal
        // toItem: 参照的view
        // attribute: 参照的view的约束
        //转轮的x和y
        addConstraint(NSLayoutConstraint(item:iconView,attribute:NSLayoutAttribute.CenterX,relatedBy:NSLayoutRelation.Equal,toItem:self,attribute:NSLayoutAttribute.CenterX,multiplier:1,constant:0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -15))
        
        //遮罩视图的x和y
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //房子的x和y
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //消息的x和y
         addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
         addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //消息的文本宽高约束
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        

        //注册的x和y
         addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
         addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //注册的宽高
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登录的x和y
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //登录的宽高
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

    }
    
    //访客视图修改页面
    func setupVisitorView(imageName:String,message:String){
        //设置文字和图片
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        //让文字适应大小
        messageLabel.sizeToFit()
        //隐藏房子
        homeView.hidden = true
        //隐藏遮盖视图
        coverView.hidden = true
    }

    //懒加载
    //添加转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //消息
    private lazy var messageLabel:UILabel = {
       let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        
        //设置文本内容
        label.text = "关注一些人,看看有神马惊喜"
        label.numberOfLines = 0
        
        //根据文本来适应大小
        label.sizeToFit()
        
        return label
        
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        //设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
       //设置文本
        button.setTitle("注册", forState: UIControlState.Normal)
        //设置文本颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //设置文本的大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //根据内容适应发小
        button.sizeToFit()
        
        return button
    }()

    //登录按钮
    private lazy var loginButton:UIButton = {
        let button = UIButton()
        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置文本
        button.setTitle("登录", forState: UIControlState.Normal)
        //设置文本颜色
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        //设置文本大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //根据内容适应
        button.sizeToFit()
        
        return button
        
    }()
    
    //遮罩视图
    private lazy var coverView:UIImageView = {
       let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
    }()
    

}
